<?php

namespace api\controllers;

use yii;
use common\models\Product;
use common\models\ProductBranch;

class ProductController extends ApiController{
    
    /**
     * 
     */
    public function actionIndex(){
        $objects = [];
        $sql = "SELECT product.*,product_branch.quantity FROM product INNER JOIN product_branch ON product.id_product = product_branch.id_product ";
        $sql .= "WHERE id_company = :id_company AND id_branch = :id_branch";
        $command = \Yii::$app->getDb()->createCommand($sql)
                ->bindValue(':id_company', \Yii::$app->request->post('brand'))
                ->bindValue(':id_branch', $this->userData->id_company);
        $products = $command->queryAll();
        
        foreach($products as $product){
            $objects[] = [
                'id_product' => $product['id_product'],
                'code' => $product['code'],
                'name' => $product['name'],
                'price' => $product['price'],
                'origin_price' => $product['origin_price'],
                'total_quantity' => $product['quantity']
            ];
        }
        
        $result = ['error' => false,'message' => '','objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionUpload(){
        $data = $this->uploadFile('file', \Yii::$app->params['PathImageProductTmp']);
        $error = strlen($data['message'])?true:false;
        $message = $data['message'];
        $result = ['error' => $error,'message' => $message,'objects' => ['file' => $data['file']]];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionSave(){
        $data = \Yii::$app->request->post();
        $product = new Product();
        $product->attributes = $data;
        $product->id_company = $this->userData->id_company;
        $product->created = $product->updated = date('Y-m-d H:i:s');
        $messages = [];
        $error = false;
        if($product->save()){
            $product->moveImage();
            foreach($this->userData->brands as $brand){
                $productBrand = new ProductBranch();
                $productBrand->id_branch = $brand->id_branch;
                $productBrand->id_product = $product->id_product;
                $productBrand->quantity = $this->userData->id_branch == $brand->id_branch?$product->total_quantity:0;
                $productBrand->created = $productBrand->updated = date('Y-m-d H:i:s');
                $productBrand->save(false);
            }
        }else{
           $error = true;
           $messages = $product->getErrors(); 
        }
        $result = ['error' => $error,'message' => $this->parserMessage($messages),'objects' => []];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionDetail(){
        $error = false;
        $messages = "";
        $objects = [];
        $id_product = \Yii::$app->request->post('id_product');
        $product = Product::find()->andWhere(['id_product' => $id_product])->with(['categoryCompany','category'])->one();
        if($product){
            $objects = [
                'id_product' => $product->id_product,
                'category' => $product->category->name,
                'category_company' => $product->categoryCompany->name,
                'code' => $product->code,
                'price' => number_format($product->price),
                'origin_price' => number_format($product->origin_price),
                'product_type' => $product->product_type,
                'type' => $product->getTextType(),
                'image' => $product->getUrlImage(),
            ];
        }
        $result = ['error' => $error,'message' => $this->parserMessage($messages),'objects' => $objects];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
}